//
//  WbVisitorView.swift
//  Weibo
//
//  Created by HH on 16/12/1.
//  Copyright © 2016年 Weibo. All rights reserved.
//

import UIKit
import SnapKit

enum LoginOrSignUp {
    case Login
    
    case SignUp
}

class WbVisitorView: WbBaseView {
    
    var callBack:((LoginOrSignUp)->())?
    
    lazy private var iconView:UIImageView = UIImageView(image:#imageLiteral(resourceName: "visitordiscover_feed_image_house") )
    
    lazy private var animateView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    
    lazy private var maskImageView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    
    lazy private var descLabel:UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人,回这里看看有什么惊喜"
        
        label.textColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        
        label.font = UIFont.systemFont(ofSize: 12)
        
        label.numberOfLines = 0
        
        label.textAlignment = NSTextAlignment.center
        
        return label
    }()
    
    lazy private var leftButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("去关注", for: .normal)
        btn.setTitleColor(UIColor.orange, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white_disable"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white_highlighted.png"), for: .highlighted)
        btn.adjustsImageWhenHighlighted = false
        
        return btn
        
    }()
    
    lazy private var rightButton:UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登录", for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white_disable"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white_highlighted.png"), for: .highlighted)
        btn.adjustsImageWhenHighlighted = false
        
        return btn
        
    }()
    

    
    
     override func setupUI(){
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
//        self.backgroundColor = UIColor.red
        
        addSubview(animateView)
        addSubview(maskImageView)
        addSubview(iconView)
        addSubview(descLabel)
        addSubview(leftButton)
        addSubview(rightButton)
        
        rightButton.addTarget(self, action: #selector(loginAction(sender:)), for: .touchUpInside)
        
        
        
        animateView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        maskImageView.snp.makeConstraints { (make) in
            make.center.equalTo(animateView)
        }
        
        iconView.snp.makeConstraints { (make) in
            make.center.equalTo(animateView)
        }
        
        descLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(224)
            make.top.equalTo(animateView.snp.bottom).offset(15)
        }
        
        leftButton.snp.makeConstraints { (make) in
            make.left.equalTo(descLabel)
            make.top.equalTo(descLabel.snp.bottom).offset(20)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        rightButton.snp.makeConstraints { (make) in
            make.right.equalTo(descLabel)
            make.top.equalTo(leftButton)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
    }
    
    func updateInfo(title:String?,image:UIImage?){
        
        if title != nil,image != nil {
            descLabel.text = title
            iconView.image = image
            leftButton.setTitle("注册", for: .normal)
            
            animateView.isHidden = true
            
            leftButton.snp.remakeConstraints { (make) in
                make.left.equalTo(descLabel)
                make.top.equalTo(descLabel.snp.bottom).offset(20)
                make.size.equalTo(CGSize(width: 80, height: 35))
            }
            
        }else{
            
            rightButton.isHidden = true
            
            leftButton.snp.remakeConstraints({ (make) in
                make.top.equalTo(descLabel.snp.bottom).offset(20)
                make.size.equalTo(CGSize(width: 80, height: 35))
                make.centerX.equalTo(descLabel)
            })
//            rightButton.setTitle("登录", for: .normal)
            startAnimation()
        }
        
    }
    
    func startAnimation(){
        
        let base = CABasicAnimation(keyPath: "transform.rotation")
        
        base.toValue = 2 * M_PI
        
        base.duration = 20.0
        
        base.repeatCount = MAXFLOAT
        
        base.isRemovedOnCompletion = false
        
        animateView.layer.add(base, forKey: "base")
        
    
    }
    
    @objc private func loginAction(sender:UIButton){
        
        
        callBack?(.Login)
    }
    
    @objc private func signUpAction(sender:UIButton){
        
        
        callBack?(.SignUp)
    }
    

}
